package com.dw.etl.core.controller;

import javax.servlet.http.HttpServletRequest;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.bean.model.BaseDb;
import com.dw.etl.core.bean.vo.LayMsg;
import org.springframework.beans.factory.annotation.Autowired;
import com.dw.etl.core.api.EtlJbService;
import com.dw.etl.core.bean.model.EtlJb;
import org.springframework.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * etlJb控制器
 * Created by hozeData on 2021/05/24.
 */
@RestController
@RequestMapping("/etl/etlJb/")
@Api(tags="ETL任务管理")
@CrossOrigin(origins = "*", maxAge = 3600)
public class EtlJbController {

    @Autowired
    EtlJbService etlJbService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.POST)
    public BaseResult findByPage(@RequestBody EtlJb etlJb, Integer pageNum, Integer pageSize) {
        PaginationVO<EtlJb> pageVO = etlJbService.findPageByModel(etlJb, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.GET)
    public LayMsg find(Integer page, Integer limit) {
        List<EtlJb> pageVO = etlJbService.findByModel(new EtlJb());
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="etlJb", value="实体", paramType="body", required=true, dataType="EtlJb")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody EtlJb etlJb, HttpServletRequest request)  {
        int num ;
        num = etlJbService.insertSelective(etlJb);
        /*if(etlJb.getIdJb()==null) {
            num = etlJbService.insertSelective(etlJb);
        } else {
            num = etlJbService.updateByIdSelective(etlJb);
        }*/
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idJb", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(String idJb, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idJb)){
            throw new BaseException("请选择要编辑的项");
        }
        EtlJb etlJb = etlJbService.getById(idJb);
        return new BaseResult(CommonResultConstant.SUCCESS, etlJb);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="etlJb", value="实体", paramType="body", required=true, dataType="EtlJb")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody EtlJb etlJb, HttpServletRequest request)  {
        int num = etlJbService.updateByIdSelective(etlJb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idJb", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(String idJb, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idJb)){
            throw new BaseException("请选择要删除的项");
        }
        int num = etlJbService.deleteById(idJb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
